/*
 *  mesh.cpp
 *  gmit
 *
 *  Created by DONG Li on 4/10/10.
 *  Copyright 2010 LASG/IAP. All rights reserved.
 *
 */

#include <stdexcept>
#include <cmath>

// ************************************************************************ Grid
template <typename T>
bool Grid<T>::isDuplicate(const Grid<T>& grid) const
{
    const vector<double> x = grid.getLocation();
    if (this->x.size() != x.size())
        throw runtime_error("Grid::isDuplicate: "
                            "Dimension not equal, can not compare.");
    for (int i = 0; i < x.size(); ++i)
        if (abs(x[i]-this->x[i]) > 10E-6)
            return false;
    return true;
}

// ************************************************************************ Edge
void Edge::addVertexGlobalId(int i)
{
    this->vertexId.push_back(i);
    // keep the vertex id in ascendent order for comparing convenience
    sort(this->vertexId.begin(), this->vertexId.end());
}

bool Edge::isDuplicate(const Edge& edge) const
{
    int n = edge.getNumVertex();
    if (n != this->getNumVertex()) {
        return false;
    }
    for (int i = 0; i < n; ++i) {
        int id1 = edge.getVertexGlobalId(i);
        int id2 = this->getVertexGlobalId(i);
        if (id1 != id2) {
            return false;
        }
    }
    return true;
}

// ************************************************************************ Cell

// ********************************************************************* BndCell